/*---------------------------------------------------------------------------------------------------------*/
/* Nuvoton Electronics Corporation confidential                                                            */
/*                                                                                                         */
/* Copyright (c) 2008 by Nuvoton Electronics Corporation                                                   */
/* All rights reserved                                                                                     */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/
/* File Contents:                                                                                          */
/*   gpio_lib.c                                                                                            */
/*            This file contains the APIs of GPIO                                                          */
/*                                                                                                         */
/* Version:                                                                                                */
/*            v0.0.2                                                                                       */
/*                                                                                                         */
/* Project:                                                                                                */
/*            DA8205                                                                                       */
/*                                                                                                         */
/*                                                                               $Date: 08/08/22 9:04p $   */
/*---------------------------------------------------------------------------------------------------------*/

#ifndef GPIO_LIB_H
#define GPIO_LIB_H

#ifndef GPIO_ERR_PORT_BUSY
/*---------------------------------------------------------------------------------------------------------*/
/* Macro, type and constant definitions                                                                    */
/*---------------------------------------------------------------------------------------------------------*/
#define GPIO_ERR_PORT_BUSY      -1
#define GPIO_ERR_UNSUPPORTED    -2
#define GPIO_ERR_BIT_BUSY       -3

#define SUCCESSFUL              0
#endif


#define GPIO_SET(port, bitmap)
#define GPIO_CLR(port, bitmap)
#define GPIO_GET_PORT(port)

#define GPIO_SET_BIT(port, bit)
#define GPIO_CLR_BIT(port, bit)
#define GPIO_GET_BIT(port, bit)

#define GPIOC_MASK  0x00007FFF
#define GPIOD_MASK  0x000007FF
#define GPIOE_MASK  0x00003FFF
#define GPIOF_MASK  0x000003FF
#define GPIOG_MASK  0x0001FFFF
#define GPIOH_MASK  0x000000FF
#define GPIOI_MASK  0x0001FFFF

typedef INT32 (*GPIO_CALLBACK)(UINT32 status, UINT32 userData);

typedef enum {
    GPIOC=0x00, 
    GPIOD=0x10, 
    GPIOE=0x20, 
    GPIOF=0x30,
    GPIOG=0x40,
    GPIOH=0x50,
    GPIOI=0x60
} GPIO_PORT;

typedef enum {
    BIT0 = 1, 
    BIT1 = 2, 
    BIT2 = 4, 
    BIT3 = 8, 
    BIT4 = 16, 
    BIT5 = 32, 
    BIT6 = 64, 
    BIT7 = 128, 
    BIT8 = 256, 
    BIT9 = 512, 
    BIT10 = 1024, 
    BIT11 = 2048, 
    BIT12 = 4096, 
    BIT13 = 8192, 
    BIT14 = 16384, 
    BIT15 = 32768,
    BIT16 = 65536
} GPIO_BIT;
typedef enum {
                FUNC_GPIO, FUNC_RMII, 
                FUNC_I2C0, FUNC_I2C1, FUNC_USI, FUNC_NAND, FUNC_PCI, 
                FUNC_KPI, FUNC_LCD, FUNC_SD0, FUNC_SD1, FUNC_SC0, FUNC_SC1, 
                FUNC_MEMSTICK0, FUNC_MEMSTICK1, FUNC_UART0, FUNC_UART1, FUNC_UART2,
                FUNC_UART3, FUNC_UART4, FUNC_MODEM, FUNC_XDMA0, FUNC_XDMA1,
                FUNC_PS20, FUNC_PS21, FUNC_I2S, FUNC_AC97, FUNC_PWM, FUNC_NIRQ,
                FUNC_NWDOG, FUNC_ATAPI, FUNC_USBPHY0, FUNC_USBPHY0DBG, FUNC_EXT_USBPHY0,
                FUNC_NIRQ0_3, FUNC_NIRQ4_7
              } GPIO_FUNCTION;
              
typedef enum {DIR_INPUT, DIR_OUTPUT} GPIO_DIR;
typedef enum {NO_PULL_UP, PULL_UP} GPIO_PULL;
typedef enum {DRV_LOW, DRV_HIGH} GPIO_DRV;
typedef enum {NIRQ0=0, NIRQ1, NIRQ2, NIRQ3, NIRQ4, NIRQ5, NIRQ6, NIRQ7} GPIO_NIRQ;
typedef enum {LOW, HIGH, FALLING, RISING} GPIO_TRIGGER_TYPE;

typedef struct 
{
    UINT16          bitBusyFlag[5];
    GPIO_CALLBACK   IRQCallback[9];
    UINT32          IRQUserData[9];
} GPIO_CFG;

/*---------------------------------------------------------------------------------------------------------*/
/* General GPIO multi-function initialization                                                              */
/*---------------------------------------------------------------------------------------------------------*/

INT32 GPIO_InitFunction(GPIO_FUNCTION function);

/*---------------------------------------------------------------------------------------------------------*/
/* General GPIO port function                                                                              */
/*---------------------------------------------------------------------------------------------------------*/

INT32 GPIO_Open(GPIO_PORT port, GPIO_DIR direction, GPIO_PULL pull);
INT32 GPIO_Close(GPIO_PORT port);
INT32 GPIO_Set(GPIO_PORT port, UINT32 bitMap);
INT32 GPIO_Clr(GPIO_PORT port, UINT32 bitMap);
UINT32 GPIO_ReadPort(GPIO_PORT port);
INT32 GPIO_SetPortDir(GPIO_PORT port, GPIO_DIR direction);

/*---------------------------------------------------------------------------------------------------------*/
/* General GPIO bit function                                                                               */
/*---------------------------------------------------------------------------------------------------------*/

INT32 GPIO_OpenBit(GPIO_PORT port, GPIO_BIT bit, GPIO_DIR direction, GPIO_PULL pull);
INT32 GPIO_CloseBit(GPIO_PORT port, GPIO_BIT bit);
INT32 GPIO_SetBit(GPIO_PORT port, GPIO_BIT bit);
INT32 GPIO_ClrBit(GPIO_PORT port, GPIO_BIT bit);
INT32 GPIO_ReadBit(GPIO_PORT port, GPIO_BIT bit);
INT32 GPIO_SetBitDir(GPIO_PORT port, GPIO_BIT bit, GPIO_DIR direction);

/*---------------------------------------------------------------------------------------------------------*/
/* GPIO status function                                                                                    */
/*---------------------------------------------------------------------------------------------------------*/

BOOL GPIO_BitIsUsed(GPIO_PORT port, GPIO_BIT bit);

/*---------------------------------------------------------------------------------------------------------*/
/* GPIO interrupt function                                                                                 */
/*---------------------------------------------------------------------------------------------------------*/

INT32 GPIO_EnableInt(GPIO_NIRQ nIrq, GPIO_CALLBACK callback, UINT32 userData);
INT32 GPIO_DisableInt(GPIO_NIRQ nIrq);
INT32 GPIO_SetTriggerType(GPIO_NIRQ nIrq, GPIO_TRIGGER_TYPE triggerType);
INT32 GPIO_EnableIntDebounce(GPIO_NIRQ nIrq, INT32 debounceClkSel);
INT32 GPIO_DisableIntDebounce(GPIO_NIRQ nIrq);


#endif

